﻿
//***********************************************************************
// * Module:   MsjZaJdhxpgmpzHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/29
// * Purpose: Definition of the Class MsjZaJdhxpgmpzHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjZaJdhxpgmpzHandler 的摘要说明
    /// </summary>
    public class MsjZaJdhxpgmpzHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                //对Session的处理
                var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                MsjZaJdhxpgmpz mMsjZaJdhxpgmpz = new MsjZaJdhxpgmpz();
                mMsjZaJdhxpgmpz.Gmid = Guid.NewGuid().ToString(); //主键
                mMsjZaJdhxpgmpz.Qymc = context.Request.Form["texQymc"]; //购买单位名称
                mMsjZaJdhxpgmpz.Qydz = context.Request.Form["texQydz"]; //法定代表人
                mMsjZaJdhxpgmpz.Dz = context.Request.Form["texDz"]; //地址
                mMsjZaJdhxpgmpz.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                mMsjZaJdhxpgmpz.Jbrxm = context.Request.Form["texJbrxm"]; //经办人姓名
                mMsjZaJdhxpgmpz.Sfzh = context.Request.Form["texSfzh"]; //身份证件名称及号码
                mMsjZaJdhxpgmpz.Bwfzr = context.Request.Form["texBwfzr"]; //保卫负责人
                mMsjZaJdhxpgmpz.Bgry = context.Request.Form["texBgry"]; //保管人员
                mMsjZaJdhxpgmpz.Cctj = context.Request.Form["texCctj"]; //储存条件
                mMsjZaJdhxpgmpz.Jdhxpyt = context.Request.Form["texJdhxpyt"]; //剧毒化学品用途
                mMsjZaJdhxpgmpz.GmdwFzrshyj = context.Request.Form["texGmdwFzrshyj"]; //购买单位_负责人审核意见
                mMsjZaJdhxpgmpz.GmdwQm = context.Request.Form["texGmdwQm"]; //购买单位_签名
                mMsjZaJdhxpgmpz.GmdwShsj = context.Request.Form["texGmdwShsj"]; //购买单位_审核时间(2013-01-12)
                mMsjZaJdhxpgmpz.GajgzabmShyj = context.Request.Form["texGajgzabmShyj"]; //公安机关治安部门_审核意见
                mMsjZaJdhxpgmpz.GajgzabmQm = context.Request.Form["texGajgzabmQm"]; //公安机关治安部门_签名
                mMsjZaJdhxpgmpz.GajgzabmShsj = context.Request.Form["texGajgzabmShsj"]; //公安机关治安部门_审核时间(2013-01-12)
                mMsjZaJdhxpgmpz.GajgfzrShyj = context.Request.Form["texGajgfzrShyj"]; //公安机关负责人_审核意见
                mMsjZaJdhxpgmpz.GajgfzrQm = context.Request.Form["texGajgfzrQm"]; //公安机关负责人_签名
                mMsjZaJdhxpgmpz.GajgfzrShsj = context.Request.Form["texGajgfzrShsj"]; //公安机关负责人_审核时间(2013-01-12)
                mMsjZaJdhxpgmpz.Bz = context.Request.Form["texBz"]; //备注
                mMsjZaJdhxpgmpz.Cjsj = DateTime.Now.ToString(); //创建时间(2013-05-28 09:13:01)
                mMsjZaJdhxpgmpz.Cjr = mUser.AppuserId; //创建人(人员ID)
                mMsjZaJdhxpgmpz.Cjrdw = mOrg.OrgnaCode; //创建单位(单位ID)
                mMsjZaJdhxpgmpz.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                mMsjZaJdhxpgmpz.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjZaJdhxpgmpzBLL.Insert(mMsjZaJdhxpgmpz, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                MsjZaJdhxpgmpz mMsjZaJdhxpgmpz = new MsjZaJdhxpgmpz();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = MsjZaJdhxpgmpzBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjZaJdhxpgmpz mMsjZaJdhxpgmpz = new MsjZaJdhxpgmpz();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjZaJdhxpgmpz = MsjZaJdhxpgmpzBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjZaJdhxpgmpz != null)
                {
                    //对Session的处理
                    var mUser = AppOrganizatiOnUserBLL.Load(context.Session["UserID"].ToString());
                    var mOrg = AppOrganizatiOnBLL.Load(mUser.OrgnaId);
                    mMsjZaJdhxpgmpz.Qymc = context.Request.Form["texQymc"]; //购买单位名称
                    mMsjZaJdhxpgmpz.Qydz = context.Request.Form["texQydz"]; //法定代表人
                    mMsjZaJdhxpgmpz.Dz = context.Request.Form["texDz"]; //地址
                    mMsjZaJdhxpgmpz.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                    mMsjZaJdhxpgmpz.Jbrxm = context.Request.Form["texJbrxm"]; //经办人姓名
                    mMsjZaJdhxpgmpz.Sfzh = context.Request.Form["texSfzh"]; //身份证件名称及号码
                    mMsjZaJdhxpgmpz.Bwfzr = context.Request.Form["texBwfzr"]; //保卫负责人
                    mMsjZaJdhxpgmpz.Bgry = context.Request.Form["texBgry"]; //保管人员
                    mMsjZaJdhxpgmpz.Cctj = context.Request.Form["texCctj"]; //储存条件
                    mMsjZaJdhxpgmpz.Jdhxpyt = context.Request.Form["texJdhxpyt"]; //剧毒化学品用途
                    mMsjZaJdhxpgmpz.GmdwFzrshyj = context.Request.Form["texGmdwFzrshyj"]; //购买单位_负责人审核意见
                    mMsjZaJdhxpgmpz.GmdwQm = context.Request.Form["texGmdwQm"]; //购买单位_签名
                    mMsjZaJdhxpgmpz.GmdwShsj = context.Request.Form["texGmdwShsj"]; //购买单位_审核时间(2013-01-12)
                    mMsjZaJdhxpgmpz.GajgzabmShyj = context.Request.Form["texGajgzabmShyj"]; //公安机关治安部门_审核意见
                    mMsjZaJdhxpgmpz.GajgzabmQm = context.Request.Form["texGajgzabmQm"]; //公安机关治安部门_签名
                    mMsjZaJdhxpgmpz.GajgzabmShsj = context.Request.Form["texGajgzabmShsj"]; //公安机关治安部门_审核时间(2013-01-12)
                    mMsjZaJdhxpgmpz.GajgfzrShyj = context.Request.Form["texGajgfzrShyj"]; //公安机关负责人_审核意见
                    mMsjZaJdhxpgmpz.GajgfzrQm = context.Request.Form["texGajgfzrQm"]; //公安机关负责人_签名
                    mMsjZaJdhxpgmpz.GajgfzrShsj = context.Request.Form["texGajgfzrShsj"]; //公安机关负责人_审核时间(2013-01-12)
                    mMsjZaJdhxpgmpz.Bz = context.Request.Form["texBz"]; //备注
                    mMsjZaJdhxpgmpz.Xgr = mUser.AppuserId; //修改人(人员ID)
                    mMsjZaJdhxpgmpz.Xgrdw = mOrg.OrgnaCode; //修改人单位(单位ID)
                    mMsjZaJdhxpgmpz.Xgsj = DateTime.Now.ToString(); //修改时间(2013-05-28 09:13:01)
                    mMsjZaJdhxpgmpz.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                    mMsjZaJdhxpgmpz.Movesign = "0"; //是否传输（1已，0未）

                    mResultModel = MsjZaJdhxpgmpzBLL.Update(mMsjZaJdhxpgmpz, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                MsjZaJdhxpgmpz mMsjZaJdhxpgmpz = new MsjZaJdhxpgmpz(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjZaJdhxpgmpz = MsjZaJdhxpgmpzBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjZaJdhxpgmpz != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjZaJdhxpgmpz);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string unit = context.Request.Form["texUnit"];
            string phone = context.Request.Form["texPhone"];
            string timeStart = context.Request.Form["texTimeStart"];
            string timeEnd = context.Request.Form["texTimeEnd"];
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtMsjZaJdhxpgmpz = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            SqlConditionList sqllist = new SqlConditionList();
            string sqlWhere = " enabled='1' ";
            if (!string.IsNullOrEmpty(unit)) ////购买单位名称
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjZaJdhxpgmpzConst.Columns.Qymc, '%' + unit + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(phone)) ////单位电话
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjZaJdhxpgmpzConst.Columns.Lxdh, '%' + phone + '%'), SqlLogicOper.AND_LOGIC);
            }

            if (!string.IsNullOrEmpty(timeStart)) ////添加时间-开始
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, MsjZaJdhxpgmpzConst.Columns.Cjsj, Convert.ToDateTime(timeStart)), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(timeEnd)) ////添加时间-结束
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, MsjZaJdhxpgmpzConst.Columns.Cjsj, Convert.ToDateTime(timeEnd)), SqlLogicOper.AND_LOGIC);
            }

            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where" + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }
            dtMsjZaJdhxpgmpz = Q.GetRecordByPage("GMID,QYMC,QYDZ,LXDH,CJSJ", sqlWhere, " cjsj", intPageSize, intPageIndex, MsjZaJdhxpgmpzConst.TableName, MsjZaJdhxpgmpzConst.FieldKey, "desc").Tables[0];
            if (dtMsjZaJdhxpgmpz != null && dtMsjZaJdhxpgmpz.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjZaJdhxpgmpz.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjZaJdhxpgmpz.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtMsjZaJdhxpgmpz;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }
        #endregion

    }
}


